package com.hejia.alauda.entity;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

import org.joda.time.LocalDate;
import org.joda.time.Period;
import org.joda.time.PeriodType;

import com.hejia.alauda.Constants;

/**
 * 产品兑付
 * 
 * @author hyr
 *
 */
public class ProductCash {

	private Long productId; // 产品ID
	private String productName; // 产品名称
	private BigDecimal totalAmount; // 产品发售金额
	private BigDecimal soldAmount; // 产品实售金额
	private String payGateCode; // 支付网关代码
	private Date startTime; // 起售时间
	private Date endTime; // 结束销售时间
	private Date saleDate; // 销售日
	private Date payDate; // 打款日，为销售日第二天，遇节假日顺延
	private Integer term; // 期限
	private Date expectRepaymentDate; // 预期兑付日
	private Date actualRepaymentDate; // 实际兑付日
	private Integer actualOccupancy; // 实际占用时间
	private Map<String, BigDecimal> payGateSoldAmount; // 支付网关实售金额，Key：支付网关代码
														// Value：实售金额

	public Long getProductId() {
		return productId;
	}

	public void setProductId(Long productId) {
		this.productId = productId;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public BigDecimal getTotalAmount() {
		return totalAmount;
	}

	public void setTotalAmount(BigDecimal totalAmount) {
		this.totalAmount = totalAmount;
	}

	public BigDecimal getSoldAmount() {
		return soldAmount;
	}

	public void setSoldAmount(BigDecimal soldAmount) {
		this.soldAmount = soldAmount;
	}

	public String getPayGateCode() {
		return payGateCode;
	}

	public void setPayGateCode(String payGateCode) {
		this.payGateCode = payGateCode;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public Date getSaleDate() {
		return saleDate;
	}

	public void setSaleDate(Date saleDate) {
		this.saleDate = saleDate;
	}

	public Date getPayDate() {
		return payDate;
	}

	public void setPayDate(Date payDate) {
		this.payDate = payDate;
	}

	public Integer getTerm() {
		return term;
	}

	public void setTerm(Integer term) {
		this.term = term;
	}

	public Date getExpectRepaymentDate() {
		return expectRepaymentDate;
	}

	public void setExpectRepaymentDate(Date expectRepaymentDate) {
		this.expectRepaymentDate = expectRepaymentDate;
	}

	/**
	 * 实际兑付遇周末节假日提前兑付
	 * 
	 * @return
	 */
	public Date getActualRepaymentDate() {
		return this.actualRepaymentDate;
	}

	public void setActualRepaymentDate(Date actualRepaymentDate) {
		this.actualRepaymentDate = actualRepaymentDate;
	}

	public Integer getActualOccupancy() {
		if (this.actualOccupancy == null) {
			Period period = new Period(new LocalDate(this.getPayDate()),
					new LocalDate(this.getActualRepaymentDate()),
					PeriodType.days());
			actualOccupancy = period.getDays();
		}
		return actualOccupancy;
	}

	public void setActualOccupancy(Integer actualOccupancy) {
		this.actualOccupancy = actualOccupancy;
	}

	public Map<String, BigDecimal> getPayGateSoldAmount() {
		if (payGateSoldAmount == null) {
			payGateSoldAmount = new TreeMap<>();
		}
		return payGateSoldAmount;
	}

	public void setPayGateSoldAmount(Map<String, BigDecimal> payGateSoldAmount) {
		this.payGateSoldAmount = payGateSoldAmount;
	}

	/**
	 * 添加支付网关金额
	 * 
	 * @param payGateCode
	 *            支付网关代码
	 * @param amount
	 *            金额
	 */
	public void addPayGateAmount(String payGateCode, BigDecimal amount) {
		if (payGateCode == null) {
			payGateCode = Constants.PAYGATE_CODE_LLPAY;
		}
		Map<String, BigDecimal> map = this.getPayGateSoldAmount();
		if (map.containsKey(payGateCode)) {
			map.put(payGateCode, map.get(payGateCode).add(amount));
		} else {
			map.put(payGateCode, amount);
		}
	}

}
